package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedDeviceParam;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaDeviceRelatedDeviceParamDao {

    /**
     * 根据 设备资源id查询
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Results(id = "pcbaDeviceRelatedDeviceParamResultMap", value = {
            @Result(column = "pcba_device_related_device_param_id", property = "pcbaDeviceRelatedDeviceParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_resource_id", property = "pcbaDeviceResourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_key", property = "pcbaDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_value", property = "pcbaDeviceParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_device_param_value_audit", property = "pcbaDeviceParamValueAudit", jdbcType = JdbcType.VARCHAR),
    })
    //@Select("select t1.* from " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " t1 left join " + DB1TableName.PCBA_DEVICE_PARAM + " t2 on t1.pcba_device_param_key = t2.pcba_device_param_key where t1.pcba_device_resource_id = #{pcbaDeviceResourceId} order by t2.pcba_device_param_type_id,t2.sort,t2.pcba_device_param_id")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " where pcba_device_resource_id = #{pcbaDeviceResourceId} order by pcba_device_related_device_param_id")
    List<PcbaDeviceRelatedDeviceParam> getAllByDeviceResourceId(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 根据id查询
     *
     * @param pcbaDeviceRelatedDeviceParamId
     * @return
     */
    @ResultMap(value = "pcbaDeviceRelatedDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " where pcba_device_related_device_param_id #{pcbaDeviceRelatedDeviceParamId}")
    PcbaDeviceRelatedDeviceParam getPcbaDeviceRelatedDeviceParamById(@Param("pcbaDeviceRelatedDeviceParamId") Integer pcbaDeviceRelatedDeviceParamId);

    /**
     * 新增
     *
     * @param pcbaDeviceRelatedDeviceParam
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + "(pcba_device_resource_id,pcba_device_param_key,pcba_device_type_id,pcba_device_param_value,pcba_device_param_value_audit,create_time) values " +
            "(#{pcbaDeviceRelatedDeviceParam.pcbaDeviceResourceId},#{pcbaDeviceRelatedDeviceParam.pcbaDeviceParamKey},#{pcbaDeviceRelatedDeviceParam.pcbaDeviceTypeId},#{pcbaDeviceRelatedDeviceParam.pcbaDeviceParamValue},#{pcbaDeviceRelatedDeviceParam.pcbaDeviceParamValueAudit},#{pcbaDeviceRelatedDeviceParam.createTime})")
    Integer addDeviceRelatedParam(@Param("pcbaDeviceRelatedDeviceParam") PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam);

    /**
     * 更新
     *
     * @param pcbaDeviceRelatedDeviceParam
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM +
            "<set>" +
            "<if test='pcbaDeviceRelatedDeviceParam.pcbaDeviceResourceId != null and pcbaDeviceRelatedDeviceParam.pcbaDeviceResourceId != \"\" '>" +
            " pcba_device_resource_id = #{pcbaDeviceRelatedDeviceParam.pcbaDeviceResourceId}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedDeviceParam.pcbaDeviceParamKey != null and pcbaDeviceRelatedDeviceParam.pcbaDeviceParamKey != \"\" '>" +
            " pcba_device_param_key = #{pcbaDeviceRelatedDeviceParam.pcbaDeviceParamKey}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedDeviceParam.pcbaDeviceTypeId != null and pcbaDeviceRelatedDeviceParam.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaDeviceRelatedDeviceParam.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedDeviceParam.pcbaDeviceParamValue != null '>" +
            " pcba_device_param_value = #{pcbaDeviceRelatedDeviceParam.pcbaDeviceParamValue}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedDeviceParam.pcbaDeviceParamValueAudit != null '>" +
            " pcba_device_param_value_audit = #{pcbaDeviceRelatedDeviceParam.pcbaDeviceParamValueAudit}," +
            "</if>" +
            "<if test='pcbaDeviceRelatedDeviceParam.updateTime != null '>" +
            " update_time = #{pcbaDeviceRelatedDeviceParam.updateTime}," +
            "</if>" +
            "</set>" +
            " where pcba_device_related_device_param_id = #{pcbaDeviceRelatedDeviceParam.pcbaDeviceRelatedDeviceParamId}" +
            "</script>")
    Integer updateDeviceRelatedParam(@Param("pcbaDeviceRelatedDeviceParam") PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam);

    /**
     * 根据pcbaDeviceResourceId 删除
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    Integer deleteDeviceRelatedParam(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 根据 设备资源id + key 查询
     *
     * @param pcbaDeviceParamkey
     * @param pcbaDeviceResourceId
     * @return
     */
    @ResultMap(value = "pcbaDeviceRelatedDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " where pcba_device_resource_id = #{pcbaDeviceResourceId} and pcba_device_param_key = #{pcbaDeviceParamkey} limit 1")
    PcbaDeviceRelatedDeviceParam getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(@Param("pcbaDeviceParamkey") String pcbaDeviceParamkey, @Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 清空对应资源的临时值（审核通过）
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " set pcba_device_param_value_audit = '' where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    Integer clearValueAudit(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 将 value 还原成 ValueAudit 的值（审核不通过）
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_RELATED_DEVICE_PARAM + " set pcba_device_param_value = pcba_device_param_value_audit where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    Integer returnValueFromValueAudit(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);
}
